/*
    This file is part of tPortal.

    tPortal is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    tPortal is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with tPortal.  If not, see <http://www.gnu.org/licenses/>.
    
    The original code was written by Sebastien Bettinger <sebastien.bettinger@gmail.com>

 */

package net.tinyportal.javax.portlet;

import java.io.File;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Enumeration;
import java.util.Properties;
import java.util.Set;

import javax.portlet.PortletContext;
import javax.portlet.PortletRequestDispatcher;
import javax.servlet.ServletContext;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.context.ServletContextAware;

public class TpPortletContext implements PortletContext, ServletContextAware  {
	@Autowired
	ServletContext context;
		
	Properties attribute = new Properties();
	Properties initParameter = new Properties();

	String portletPath;
	String portletName;
	
	public TpPortletContext() {}

    public void setServletContext(ServletContext context) {
    	this.context = context;
    }
    
	public ServletContext getServletContext() {
		return context;
	}

	/**
	 * Positionne le chemin du portlet
	 * @param portletPath Chemin relatif du portlet
	 */
	public void setPortletPath(String portletPath) {
		File f = new File(context.getRealPath(portletPath));
		if (!f.isDirectory()) {
			
		}
		File contextPath = new File(context.getRealPath("/"));
		this.portletPath = f.toString().substring(contextPath.toString().length()).replaceAll("\\\\", "/");
	}

	/**
	 * Positionne le nom du portlet
	 * @param portletName
	 */
	public void setPortletName(String portletName) {
		this.portletName = portletName;
	}


    @Override
	public PortletRequestDispatcher getRequestDispatcher(String path) {
		return new TpPortletRequestDispatcher(context.getRequestDispatcher(portletPath+path));
	}
	
	@Override
	public PortletRequestDispatcher getNamedDispatcher(String name) {
		return new TpPortletRequestDispatcher(context.getNamedDispatcher(name));
	}

	@Override
	public InputStream getResourceAsStream(String path) {
		return context.getResourceAsStream(portletPath+path);
		
	}

	@Override
	public int getMajorVersion() {
		return 1;
	}

	@Override
	public int getMinorVersion() {
		return 0;
	}

    @Override
	public String getServerInfo() {
		return context.getServerInfo();
	}
    
	@Override
	public String getMimeType(String file) {
		return context.getMimeType(portletPath+file);
	}

	@Override
	public String getRealPath(String path) {
		return context.getRealPath(portletPath+path);
	}

	@Override
	public Set getResourcePaths(String path) {
		return context.getResourcePaths(portletPath+path);
	}

	@Override
	public URL getResource(String path) throws MalformedURLException {
		return context.getResource(portletPath+path);
	}

	@Override
	public Object getAttribute(String name) {
		return (String)attribute.getProperty(name);
	}

	@Override
	public Enumeration getAttributeNames() {
		return attribute.elements();
	}

	@Override
	public String getInitParameter(String name) {
		return (String)initParameter.getProperty(name);
	}

	@Override
	public Enumeration getInitParameterNames() {
		return initParameter.elements();
	}

	@Override
	public void log(String msg) {
		context.log(msg);		
	}

	@Override
	public void log(String message, Throwable throwable) {
		context.log(message, throwable);
	}

	@Override
	public void removeAttribute(String name) {
		attribute.remove(name);
	}

	@Override
	public void setAttribute(String name, Object object) {
		attribute.put(name, object);
	}

	@Override
	public String getPortletContextName() {
		return portletName;
	}
	
	public TpPortletContext clone() {
		TpPortletContext p = new TpPortletContext();
		p.setServletContext(context);
		return p;
	}

}
